<?php

namespace application\Controller;

use application\Comm\Cookie;
use application\Components\BaseController;
use application\Comm\Request;
use application\Module\Designer;
use application\Module\House;
use application\Module\HouseProductRelat;
use application\Module\Material;
use application\Module\Product;
use application\Module\ProductImage;
use library\Core\Config;
use library\Core\Db\CDbCriteria;
use application\Model\ProductModel;
use application\Comm\Func;
use application\Module\Adimage;
use application\Module\ProductTag;
use application\Module\Adtext;

class ProductController extends BaseController
{
    /**
     * @throws \application\Components\CException
     * 列表
     */
    public function actionIndex()
    {
    	//接收参数
    	$acreage_id = Request::getParam('acreage_id');
        $space = Request::getParam('space');
        $tag_id = Request::getParam('tag_id');
        $style_id = Request::getParam('style_id');
        $function_id = Request::getParam('function_id');
        $price_id = Request::getParam('price_id');
        $ordertype = Request::getParam('ordertype') ? Request::getParam('ordertype') : 'desc';
       // var_dump(Request::getParam('ordertype'));exit;
        $orderby = Request::getParam('orderby') ? Request::getParam('orderby') : 1;
        //读取配置文件
        $ckids = Config::getParamsVal('PRODUCT_CATEGORY_REWRITE_FILP');
        $styles = $space == 'chudian' ? [] : Config::getParamsVal('STYLE');
        $functions = Config::getParamsVal('FUNCTION');
        $prices = Config::getParamsVal('PRICE');
        $prices_all = Config::getParamsVal('PRICE_ALL');
        $product_category = Config::getParamsVal('PRODUCT_CATEGORY');
        $acreage = Config::getParamsVal('ACREAGE');
        //制作对比条件
        
        foreach ($product_category as $k =>$v){
        	foreach ($ckids as $key => $val){
        		if ($k == $val){
        			$categorys[$k]['name'] = $product_category[$k];
        			$categorys[$k]['name1'] = $key;
        		}
        		
        	}
        	
        	
        }
        
        $space_id = isset($ckids[$space]) ? $ckids[$space] : 1;//默认客厅
      
        $productTag = new ProductTag();
        $tags = [];//$productTag->getList($space_id);
        $productList = new Product();
        $products = [];
        $status = Config::getParamsVal('STATUS');
       //获取符合搜索条件的数据
        $select = [
            'id',
            'name',
            'description',
            'is_show_price',
            'price',
            'price_id',
            'view',
            'category_id',
            'status',
            'create_time',
            'update_time',
            'thumb',
            'orderby',
            'number',
        	'unit',
        ];
        $category_id = isset($ckids[$space]) ? $ckids[$space] : 0;//默认客厅
        $dataProvider = $productList->getList($select, $category_id);
       
        foreach ($dataProvider['data'] as $key => $product) {
            $products[$key]['id'] = $product['id'];
            $products[$key]['url'] = Func::createUrl('product/view', ['id' => $product['id']]);
            $products[$key]['name'] = $product['name'];
            $products[$key]['number'] = $product['number'];
            $products[$key]['view'] = $product['view'];
            $products[$key]['is_show_price'] = $product['is_show_price'];
            $product['price'] = floor($product['price']);
            $products[$key]['price'] = $product['price'];
            $products[$key]['price_id'] = $product['price_id'];
            $products[$key]['price_show'] = $product['is_show_price'] == 1 ? $product['price'] && $product['unit'] !='' ? $product['price'].$product['unit'] :$product['price'].'元' : @$prices_all[$product['category_id']][$product['price_id']]['lable'];
            $products[$key]['thumb'] = Func::imgUrl($product['thumb']);
            $products[$key]['description'] = $product['description'];
            $products[$key]['orderby'] = $product['orderby'];
            $products[$key]['status'] = isset($status[$product['status']]) ? $status[$product['status']] : '';
            $products[$key]['create_time'] = date('Y-m-d H:i:s', $product['create_time']);
            $products[$key]['update_time'] = date('Y-m-d H:i:s', $product['update_time']);
        }
        
        //获取为您推荐的数据
         
        $select = [
        		'id',
        		'name',
        		'category_id',
        		'price_id',
        		'price',
        		'thumb',
        		'is_show_price',
        		'unit',
        		];
      
        $Recommend = $productList->getRecommend($select, $category_id,4);
        $Recommends = [];
        foreach ($Recommend['data'] as $key => $v) {
        	$v['price'] = floor($v['price']);
        	$Recommends[$key]['url'] = Func::createUrl('product/view', ['id' => $v['id']]);
        	$Recommends[$key]['price'] = $v['is_show_price'] == 1 ? $v['price'] && $v['unit'] !='' ? $v['price'].$v['unit'] :$v['price'].'元' : @$prices_all[$v['category_id']][$v['price_id']]['lable'];
        	$Recommends[$key]['name'] = $v['name'];
        	$Recommends[$key]['thumb'] = Func::imgUrl($v['thumb']);
        }
     
        $this->render('product/index', [
        	'prices_all'=>$prices_all,
        	'acreage'=>$acreage,
        	'acreage_id'=>$acreage_id,
        	'category' =>$categorys,
        	'product_category'=>$product_category,
        	'Recommend'=>$Recommends,
        	'space'=>$space,
            'products' => $products,
            'space_id' => $space_id,
            'style_id' => $style_id,
            'price_id' => $price_id,
            'function_id' => $function_id,
            'functions' => $functions,
            'styles' => $styles,
            'prices' => $prices,
            'tag_id' => $tag_id,
            'orderby' => $orderby,
            'ordertype' => $ordertype,
            'tags' => $tags,
            'page' => $dataProvider['page'],
        ]);
    }

    /**
     * @param $id
     * 详情
     */
    public function actionView($id)
    {

        $status = Config::getParamsVal('STATUS');
        $category = Config::getParamsVal('PRODUCT_CATEGORY');
        $price_all = Config::getParamsVal('PRICE_ALL');
        $brand = Config::getParamsVal('PRODUCT_BRAND');
        $category_key = Config::getParamsVal('PRODUCT_CATEGORY_REWRITE');
        $criteria = new CDbCriteria();
        $select = [
            'id',
            'name',
            'description',
            'category_id',
            'designer_style',
            'is_show_price',
            'price',
            'price_id',
            'brand_id',
            'status',
            'content',
            'create_time',
            'update_time',
            'thumb',
            'orderby',
            'number',
        	'unit',
        ];
        $criteria->select($select);
        $criteria->addEqCondition('id', $id);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $productModel = new ProductModel();
        $data = $productModel->getRow($criteria);
        $product_images = $house_product_list = $product_history = $house_info = $designer_info = [];
        
        //热销排行版
        $adimage = new Adimage();
        //要根据配置文件热销id来改变这里的热销id
        $hot_list = $adimage->getList(24);
        $content_ad = $adimage->getList(23);
		
        $hotId = [];
        foreach ($hot_list as $hk => $hv){
        	$hotId[] = intval($hv['name']);	
        }
       
        $hot_data = [];
        if($hotId){
        	$productmodule = new Product();
        	$hot_data = $productmodule->getListByIds($hotId,$select);
        }
       
        //优惠活动
        $adtext = new Adtext();
        $favorable_list = $adtext->getList(15);
        

        $history = Cookie::get('product_history') ? json_decode(Cookie::get('product_history'), true) : [];
        if ($data) {
            $data['category_name'] = isset($category[$data['category_id']]) ? $category[$data['category_id']] : '';
            $data['brand_name'] = isset($brand[$data['brand_id']]) ? $brand[$data['brand_id']] : '';
            $space = isset($category_key[$data['category_id']]) ? $category_key[$data['category_id']] : 'keting';
            $data['category_url'] = Func::createUrl('product/index', ['space' => $space]);
            $data['designer_style'] = $data['designer_style'] ? Func::imgUrl($data['designer_style']) : '';
            $data['price'] = floor($data['price']);
            if ( $data['unit'] !=''){
            	$data['price'] = $data['price'].$data['unit'];
            }else {
            	$data['price'] = $data['price'].'元';
            }
        	$data['price_show'] = $data['is_show_price'] == 1 ? $data['price'] : @$price_all[$data['category_id']][$data['price_id']]['lable'];
            //历史纪录
            array_unshift($history, $id);
            $history = array_unique($history);
            Cookie::set('product_history', json_encode($history), 3600);
            array_shift($history);

            if (!empty($history)) {
                $productMoudle = new Product();
                $product_history = $productMoudle->getListByIds($history);
                arsort($product_history);
            }

            $image = new ProductImage();
            $product_images = $image->getList($id);                 //图册
            $material = new Material();
            $product_material = $material->getShow($id);            //板材

            $houseProductRelat = new HouseProductRelat();
            $house_id = $houseProductRelat->getHouseId($id);        //全屋

            if ($house_id) {
                $house = new House();
                $house_info = $house->getInfo($house_id);
                $house_product_list = $house->getShow($house_id);
                if (isset($house_info['designer_id'])) {
                    $designerModule = new Designer();
                    $designer_info = $designerModule->getInfo($house_info['designer_id']); //全屋设计师信息
                }

            }
        } else {
            $this->redirect(Func::createUrl('site/error'));
        }
        //更新产品浏览数量
        $module_product = new Product();
        $module_product->updateView($id);

        //===========================SEO三大标签==============================
        $this->title = $data['name'] . '价格_尺寸_图片_正品定制-皮阿诺定制商城';
        $this->keyword = $data['name'] . ',皮阿诺定制商城';
        $this->description = '皮阿诺定制家具,专业为你定制' . $data['name'] . '，提供具有功能性、收纳性、装饰性的定制家具服务,5大免费服务项目,一站式家具网购就在皮阿诺定制商城。';
        //===========================SEO三大标签==============================
	
        $this->render('product/view', [
        	'price' => $price_all,
            'data' => $data,
            'status' => $status,
            'product_images' => $product_images,
            'product_material' => $product_material,
            'house_product_list' => $house_product_list,
            'hot_list' => $hot_data,
            'product_history' => $product_history,
            'favorable_list' => $favorable_list,
            'house_info' => $house_info,
            'content_ad' => $content_ad,
            'designer_info' => $designer_info,
            'category' => $category,

        ]);
    }


}
